
import { setStore, getStore } from '@/utils/store'
// 重置任务创建
function resetTaskCreate() {
  return { newObj: { basic: {}, step: 1 }}
}
const historyFormData = getStore({ name: 'taskFormData' })
const state = {
  form: historyFormData ? historyFormData.form : resetTaskCreate()
}

const mutations = {
  SET_BASIC: (state, data) => {
    const obj = JSON.parse(JSON.stringify(state.form))
    if (obj[data.id]) {
      obj[data.id].basic = data.data
    } else {
      obj[data.id] = {
        basic: data.data,
        step: 1
      }
    }
    state.form = obj
  },
  SET_STEP: (state, data) => {
    const obj = JSON.parse(JSON.stringify(state.form))
    obj[data.id].step = data.step
    state.form = obj
    setStore({ name: 'taskFormData', content: state, type: true })
  },
  SET_RESET: (state, id) => {
    const obj = JSON.parse(JSON.stringify(state.form))
    if (id !== 'newObj') {
      delete obj[id]
      state.form = obj
      setStore({ name: 'taskFormData', content: state, type: true })
    } else {
      state.form.newObj = { basic: {}, step: 1 }
    }
  },
  // 重置
  SET_REST_TASKFORM: (state) => {
    const form = resetTaskCreate()
    state.form.newObj = form.newObj
    setStore({ name: 'taskFormData', content: state, type: true })
  }
}

const getters = {
  // 检测是否有未创建完的任务
  hasUnfinished: state => {
    return state.form.newObj && state.form.newObj.step !== 1
  }
}

const actions = {
  setBasic({ commit }, data) {
    commit('SET_BASIC', data)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
